package com.example.composableexample.uistate.basicstate

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.runtime.toMutableStateList
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.example.composableexample.ui.theme.ComposableExampleTheme


@Preview
@Composable
fun WellnessScreenPreview() {
    ComposableExampleTheme {
        Column(
            modifier = Modifier
                .fillMaxSize()
                .background(color = MaterialTheme.colorScheme.background)
        ) {
            val viewModel = viewModel<WellnessViewModel>()
            Column {
                WellnessScreen()
                WellnessTaskList(
                    list = viewModel.tasks,
                    onCloseTask = { task -> viewModel.remove(task) },
                    onCheckedChange = { task, checked ->
                        viewModel.changeTaskChecked(task, checked)
                    }
                )
            }
        }
    }
}


@Composable
fun WellnessScreen(modifier: Modifier = Modifier) {
    StatefulCounter(modifier = modifier)
}

@Composable
fun StatefulCounter(modifier: Modifier = Modifier) {
    var count by remember { mutableStateOf(0) }
    StatelessCounter(modifier = modifier, count = count, onIncrement = { count++ })
}

@Composable
private fun StatelessCounter(modifier: Modifier = Modifier, count: Int, onIncrement: () -> Unit) {
    if (count > 0) {
        Text(text = "You had $count glass", modifier = Modifier.padding(16.dp))
    }
    Button(
        onClick = onIncrement,
        modifier = Modifier.padding(16.dp),
        enabled = count <= 10
    ) {
        Text(text = "Add One")
    }
}

